package com.travel.admin.web.admin;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.travel.admin.constants.Token;
import com.travel.admin.domain.About;
import com.travel.admin.domain.AboutQuery;
import com.travel.admin.enums.DeleteFlagEnum;
import com.travel.admin.exception.TravelExceptionCode;
import com.travel.admin.service.AboutService;
import com.travel.admin.web.request.AboutCreateRequest;
import com.travel.admin.web.request.AboutModifyRequest;
import com.travel.admin.web.request.AboutQueryRequest;
import com.travel.admin.web.request.AboutRemoveRequest;
import com.travel.admin.web.request.Pager;
import com.travel.admin.web.request.StatusModifyRequest;
import com.travel.admin.web.request.ViewRequest;
import com.travel.admin.web.response.AboutQueryResponse;
import com.travel.admin.web.response.AboutResponse;
import com.travel.admin.web.response.HandleResp;
import com.travel.admin.web.response.NewPageResponse;

@Controller
public class AboutController extends AppController {

	@Autowired private AboutService aboutService;	
	
	@RequestMapping(value = "/v1/about/query", method = {RequestMethod.POST})
	@ResponseBody
	@Token
	public HandleResp<NewPageResponse<AboutQueryResponse>> query(@RequestBody AboutQueryRequest request) {
		NewPageResponse<AboutQueryResponse> response = new NewPageResponse<AboutQueryResponse>();
		try {
			AboutQuery aboutQuery = new AboutQuery();
			if (!this.isEmpty(request.getStatus())) {
				aboutQuery.setDeleteFlag(request.getStatus());
			}
			Integer total = this.aboutService.count(aboutQuery).getData();
			response.setTotal(total);
			response.setItems(new ArrayList<>());
			Pager pager = new Pager(total, request.getPage(), request.getLimit());
			aboutQuery.setStart(pager.getOffset());
			aboutQuery.setLimit(pager.getLimit());
			List<About> abouts = this.aboutService.find(aboutQuery).getData();
			if (abouts != null) {
				for (About about : abouts) {
					AboutQueryResponse aboutQueryResponse = new AboutQueryResponse();
					aboutQueryResponse.setId(about.getId());
					
					aboutQueryResponse.setStatus(about.getDeleteFlag());
					response.getItems().add(aboutQueryResponse);
				}
			}
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return HandleResp.successResponse(response);
	}

	@RequestMapping(value = "/v1/about/create", method = {RequestMethod.POST})
	@ResponseBody
	@Token
	public HandleResp<AboutResponse> create(@RequestBody AboutCreateRequest request) {
		AboutResponse response = new AboutResponse();
		try {
			if (this.isEmpty(request.getTitle())) {
				return HandleResp.failedResponseForGlobal(TravelExceptionCode.FAILED.getErrorCode(), "请输入标题");
			}
			if (this.isEmpty(request.getContent())) {
				return HandleResp.failedResponseForGlobal(TravelExceptionCode.FAILED.getErrorCode(), "请输入内容");
			}
			Date datetime = this.getServerTime();
			About about = new About();
			about.setTitle(request.getTitle());
			about.setContent(request.getContent());
			about.setDeleteFlag(DeleteFlagEnum.Y.getCode());
			about.setModifyTime(datetime);
			about.setCreateTime(datetime);
			this.aboutService.create(about);
			response.setId(about.getId());
			return HandleResp.successResponse(response);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return HandleResp.failedResponse(TravelExceptionCode.FAILED);
	}

	@RequestMapping(value = "/v1/about/modify", method = {RequestMethod.POST})
	@ResponseBody
	@Token
	public HandleResp<Object> modify(@RequestBody AboutModifyRequest request) {
		try {
			if (this.isEmpty(request.getId())) {
				return HandleResp.failedResponseForGlobal(TravelExceptionCode.FAILED.getErrorCode(), "请输入编号");
			}
			if (this.isEmpty(request.getName())) {
				return HandleResp.failedResponseForGlobal(TravelExceptionCode.FAILED.getErrorCode(), "请输入名称");
			}
			if (this.isEmpty(request.getUrl())) {
				return HandleResp.failedResponseForGlobal(TravelExceptionCode.FAILED.getErrorCode(), "请上传图片");
			}
			if (this.isEmpty(request.getOrder())) {
				return HandleResp.failedResponseForGlobal(TravelExceptionCode.FAILED.getErrorCode(), "请输入排序");
			}
			if (this.isEmpty(request.getStatus())) {
				return HandleResp.failedResponseForGlobal(TravelExceptionCode.FAILED.getErrorCode(), "请输入状态");
			}
			Date datetime = this.getServerTime();
			About about = new About();
			about.setId(request.getId());
			
			about.setDeleteFlag(request.getStatus());
			about.setModifyTime(datetime);
			this.aboutService.modifyById(about);
			return HandleResp.successResponse();
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return HandleResp.failedResponse(TravelExceptionCode.FAILED);
	}

	@RequestMapping(value = "/v1/about/remove", method = {RequestMethod.POST})
	@ResponseBody
	@Token
	public HandleResp<Object> remove(@RequestBody AboutRemoveRequest request) {
		try {
			if (this.isEmpty(request.getId())) {
				return HandleResp.failedResponseForGlobal(TravelExceptionCode.FAILED.getErrorCode(), "请输入编号");
			}
			Date datetime = this.getServerTime();
			About about = new About();
			about.setId(request.getId());
			about.setDeleteFlag(DeleteFlagEnum.N.getCode());
			about.setModifyTime(datetime);
			this.aboutService.modifyById(about);
			return HandleResp.successResponse();
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return HandleResp.failedResponse(TravelExceptionCode.FAILED);
	}

	@RequestMapping(value = "/v1/about/view", method = {RequestMethod.POST})
	@ResponseBody
	@Token
	public HandleResp<AboutResponse> view(@RequestBody ViewRequest request) {
		AboutResponse response = new AboutResponse();
		try {
			AboutQuery aboutQuery = new AboutQuery();
			aboutQuery.setPage(1);
			aboutQuery.setLimit(1);
			aboutQuery.setStart(0);
			// 查询
			About about = this.aboutService.find(aboutQuery).getData().get(0);
			if (about != null) {
				response.setId(about.getId());
				response.setTitle(about.getTitle());
				response.setContent(about.getContent());
			}
			return HandleResp.successResponse(response);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return HandleResp.failedResponse(TravelExceptionCode.FAILED);
	}

	@RequestMapping(value = "/v1/about/status/modify", method = RequestMethod.POST)
	@ResponseBody
	@Token
	public HandleResp<Object> modifyStatus(@RequestBody StatusModifyRequest request) {
		try {
			if (this.isEmpty(request.getId())) {
				return HandleResp.failedResponseForGlobal(TravelExceptionCode.FAILED.getErrorCode(), "请输入编号");
			}
			if (this.isEmpty(request.getStatus())) {
				return HandleResp.failedResponseForGlobal(TravelExceptionCode.FAILED.getErrorCode(), "请输入状态");
			}
			Date datetime = this.getServerTime();
			About about = new About();
			about.setId(request.getId());
			about.setDeleteFlag(request.getStatus());
			about.setModifyTime(datetime);
			this.aboutService.modifyById(about);
			return HandleResp.successResponse();
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return HandleResp.failedResponse(TravelExceptionCode.FAILED);
	}
}
